package com.renwei.yunlong.activity.contacts;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.DepartmentSelectAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.DepartBean;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class DepartmentChooseActivity extends BaseActivity {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.rlv_department)
    RecyclerView rlvDepartment;
    private String currentDepartmentId;
    private DepartmentSelectAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_department);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        currentDepartmentId = StringUtils.value(getIntent().getStringExtra("currentDepartmentId"));
        initView();
        initData();
    }

    private void initView() {
        simpleTileView.setTitle("部门选择");
        adapter = new DepartmentSelectAdapter(this, currentDepartmentId);
        rlvDepartment.setLayoutManager(new LinearLayoutManager(this));
        rlvDepartment.setAdapter(adapter);
        adapter.setOnItemClickLitener(new BaseRecyclerViewAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                DepartBean.RowsBean bean = adapter.getItem(position);
                Intent intent = new Intent();
                intent.putExtra("id", bean.getDepartmentId());
                intent.putExtra("name", bean.getDepartmentName());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {
            map.put("ownerCode", ownerBean.getRows().getOwnerCode());
        } else {
            map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
        }
        ServiceRequestManager.getManager().querDepartment(this, JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                DepartBean bean = new Gson().fromJson(result, DepartBean.class);
                if (bean.getMessage().getCode() == 200) {
                    setData(bean.getRows());
                }
                break;
            default:
                break;
        }
    }

    private void setData(List<DepartBean.RowsBean> list) {
        List<DepartBean.RowsBean> beanList = new ArrayList<>();
        for (DepartBean.RowsBean bean : list) {
            beanList.addAll(formatData(bean, 1));
        }
        adapter.setData(beanList);
    }

    private List<DepartBean.RowsBean> formatData(DepartBean.RowsBean bean, int index) {
        // 添加外层
        List<DepartBean.RowsBean> list = new ArrayList<>();
        bean.setIndex(index);
        list.add(bean);
        // 添加里面
        if (bean.getChildren() != null && bean.getChildren().size() != 0) {
            index++;
            for (DepartBean.RowsBean childBean : bean.getChildren()) {
                list.addAll(formatData(childBean, index));
            }
        }
        return list;
    }


    public static void openActivity(Activity contact, int requeCode, String currentId) {
        Intent intent = new Intent(contact, DepartmentChooseActivity.class);
        intent.putExtra("currentDepartmentId", currentId);
        contact.startActivityForResult(intent, requeCode);
    }
}